class Solution {
public:
    int countBeautifulPairs(vector<int>& nums)
    {
        int n = nums.size();
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            int tmp = nums[i];
            int m = 0;
            while (tmp != 0)
            {
                m = tmp % 10;
                tmp /= 10;
            }
            for (int j = i + 1; j < n; j++)
            {
                int a = m;
                int b = nums[j] % 10;
                int r = a % b;
                while (a % b != 0)
                {
                    a = b;
                    b = r;
                    r = a % b;
                }
                if (b == 1) ans++;
            }
        }
        return ans;
    }
};